/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.mgt.server.service.power.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.yulichang.repository.JoinCrudRepository;
import com.iwindplus.boot.domain.enums.BizCodeEnum;
import com.iwindplus.boot.domain.exception.BizException;
import com.iwindplus.mgt.domain.dto.power.OrgExtendDTO;
import com.iwindplus.mgt.server.mapper.power.OrgExtendMapper;
import com.iwindplus.mgt.server.model.power.OrgExtendDO;
import com.iwindplus.mgt.server.service.power.OrgExtendService;
import java.util.List;
import java.util.Objects;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 组织扩展业务层接口实现类.
 *
 * @author zengdegui
 * @since 2018/9/1
 */
@Service
@CacheConfig(cacheNames = {"orgExtend"})
@Transactional(rollbackFor = Exception.class)
public class OrgExtendServiceImpl extends JoinCrudRepository<OrgExtendMapper, OrgExtendDO> implements OrgExtendService {

    @Override
    public String getIntroByOrgId(Long orgId) {
        final List<OrgExtendDO> list = super.list(Wrappers.lambdaQuery(OrgExtendDO.class)
            .eq(OrgExtendDO::getOrgId, orgId).select(OrgExtendDO::getIntro));
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0).getIntro();
        }
        return null;
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {"orgExtend"}, allEntries = true),
            @CacheEvict(cacheNames = {"org"}, allEntries = true)
        }
    )
    @Override
    public boolean save(OrgExtendDTO entity) {
        final OrgExtendDO model = BeanUtil.copyProperties(entity, OrgExtendDO.class);
        super.save(model);
        entity.setId(model.getId());
        return Boolean.TRUE;
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {"orgExtend"}, allEntries = true),
            @CacheEvict(cacheNames = {"org"}, allEntries = true)
        }
    )
    @Override
    public boolean removeByOrgIds(List<Long> orgIds) {
        return CollUtil.isNotEmpty(orgIds) && SqlHelper.retBool(super.baseMapper.deleteByOrgIds(orgIds));
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {"orgExtend"}, allEntries = true),
            @CacheEvict(cacheNames = {"org"}, allEntries = true)
        }
    )
    @Override
    public boolean edit(OrgExtendDTO entity) {
        if (CharSequenceUtil.isBlank(entity.getIntro())) {
            return Boolean.FALSE;
        }
        OrgExtendDO data = super.getOne(Wrappers.lambdaQuery(OrgExtendDO.class)
            .eq(OrgExtendDO::getOrgId, entity.getOrgId()));
        if (Objects.isNull(data)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        final OrgExtendDO model = BeanUtil.copyProperties(entity, OrgExtendDO.class);
        super.updateById(model);
        return Boolean.TRUE;
    }
}
